package com.hot100.mid;

/**
 * @author zhengTao
 * @version 1.0
 * @description: 验证二叉搜索树
 * @date 2022/8/21 14:55
 */
public class Demo26 {
    /**
     * Definition for a binary tree node.
     * */
     public class TreeNode {
         int val;
         TreeNode left;
         TreeNode right;
         TreeNode() {}
         TreeNode(int val) { this.val = val; }
         TreeNode(int val, TreeNode left, TreeNode right) {
             this.val = val;
             this.left = left;
             this.right = right;
         }
     }
     //错解
    public boolean isValidBST(TreeNode root) {
      /*if(root==null){
          return true;
      }
        boolean left;
        boolean right;
      if(root.left==null||root.val>root.left.val){
          left=isValidBST(root.left);
      }else {
          left=false;
      }
      if(root.right==null||root.val<root.right.val){
          right=isValidBST(root.right);
      }else {
          right=false;
      }
      return left&&right;*/
        return validate(root, Long.MIN_VALUE, Long.MAX_VALUE);
    }
    public boolean validate(TreeNode node, long min, long max) {
        if (node == null) {
            return true;
        }
        if (node.val <= min || node.val >= max) {
            return false;
        }
        return  validate(node.left, min, node.val) && validate(node.right, node.val, max);
    }
}
